﻿using System.IO;
using System.Text;
namespace MLogix
{
    sealed public class FileIO
    {
        public static void ParseAndLoadFilters(FileInfo f, string cat)
        {
            StreamReader sr = f.OpenText();
            string[] filters = sr.ReadToEnd().Replace("\r", "").Split('\n');
            sr.Close();
            sr.Dispose();
            foreach (string s in filters)
            {
                if (!string.IsNullOrEmpty(s.Trim()))
                {
                    if (cat == "C")
                    {
                        DBOPS.SenseFilterDB.AddContactFilter(s);
                    }
                    if (cat == "M")
                    {
                        DBOPS.SenseFilterDB.AddMessageFilter(s);
                    }

                }
            }
        }

        public static bool SaveCSV(System.Collections.Generic.List<string> CSVList,string contactName)
        {
            bool result = false;
            Encoding swEncoding;
            System.Windows.Forms.SaveFileDialog S = new System.Windows.Forms.SaveFileDialog();
            try
            {
                swEncoding = Encoding.Unicode;
                S.FileName = contactName + ".csv";
                S.Filter = "csv files (*.csv)|*.csv";
                if (S.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    StreamWriter sw = new StreamWriter(S.FileName, false, swEncoding);
                    foreach (string line in CSVList)
                    {
                        sw.WriteLine(line);
                    }
                    sw.Close();
                    sw.Dispose();
                    result = true;
                }
            }
            catch
            {
                result = false;
            }
            S.Dispose();
            return result;
        }

    }
}

